<?php
class User_Permission extends CI_Controller {
	private $error = array();
 
	public function index() {
		$this->lang->load('user/user_group');
 
		$this->document->setTitle($this->lang->line('heading_title'));
		
		$script = base_url()."includes/js/easyui/jquery.easyui.min.js";
		$this->document->addScript($script);
		
		$style_one = base_url()."includes/js/easyui/themes/default/easyui.css";
		$style_two = base_url()."includes/js/easyui/themes/icon.css";
		
		$this->document->addStyle($style_one);
		$this->document->addStyle($style_two);
		
		$this->load->model('setting/Model_Config');
 		
		$this->load->model('user/Model_User_Group');
		
		$this->getList();
	}
	
	function datagrid()
	{
    	$this->lang->load('user/user');	
		$this->load->model('setting/Model_Config');
		$this->load->model('user/Model_User_Group');
		
		
		if($this->input->post('page'))
		{
			$page = (int)$this->input->post('page');
			$rows = (int)$this->input->post('rows');
		}
		else
		{
			$page = 1;
			$rows = 10;
		}
		
		$array_one = array();
		
			$sort = $this->input->post('sort');
			
			switch($sort)
			{
				case "user_group_id":				
					$sortone = "u.user_group_id ".$this->input->post('order');
					break;
				case "name":
					$sortone = "u.user_group_name ".$this->input->post('order');
					break;
				
				
				default:
					$sortone = "u.user_group_id ".$this->input->post('order');
					
			}
			
				
			
		$arrayone = array();
		$keyarray = array();	
			
		
		$offset = $rows*($page-1);
		$num = $rows;
		
		$language_id = $this->Model_Config->getLanguageID($this->Model_Config->getKey('config_admin_language'));
		//$language_id = $this->Model_Config->getLanguageID('en');
		
		$results = $this->Model_User_Group->get_specific_all($rows, $offset, $sortone, $keyarray, $language_id);
		
		foreach($results as $result)
		{
			$array_one[] = array(
				'user_group_id' => $result['user_group_id'],
				'name'          => $result['user_group_name']
			);
		}
		
		$total_num = $this->Model_User_Group->get_specific_count_all($keyarray);
		
		
		$array = array(
			"total" => $total_num,
			"rows" => $array_one,
		);
		
		echo json_encode($array);
	}

	public function insert() {
		$this->lang->load('user/user_group');

		$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('user/Model_User_Group');
		
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
			$this->Model_User_Group->addUserGroup($this->input->post());
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));
						
			redirect('user/user_permission');
		}

		$this->getForm();
	}

	public function update() {
		$this->lang->load('user/user_group');

		$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('user/Model_User_Group');
		
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
			$this->Model_User_Group->editUserGroup($this->uri->segment(4), $this->input->post());
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));
						
			redirect('user/user_permission');
		}

		$this->getForm();
	}

	public function delete() { 
		$success = false;
		$this->lang->load('user/user_group');
		$this->load->model('user/Model_User_Group');
		
		$selected = explode('-', $this->input->post('selected'));
		
		
		if ($selected && $this->validateDelete()) {
			foreach ($selected as $user_group_id)
			{
				$success = $this->Model_User_Group->deleteUserGroup($user_group_id);	
			}
			
			if($success)
			{
				echo "succ";
			}
			else
			{
				echo "fail1";
			}
		}
		else
		{
			echo "fail2";
		}
	}

	private function getList() {
  		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),
      		'separator' => false
   		);

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('heading_title'),
			'href'      => site_url('user/user_permission'),
      		'separator' => ' :: '
   		);
							
		$data['insert'] = site_url('user/user_permission/insert');
		$data['delete'] = site_url('user/user_permission/delete');	
		$data['edit'] = site_url('user/user_permission/update');	

		$data['datagrid'] = site_url('user/user_permission/datagrid');
	
		$data['heading_title'] = $this->lang->line('heading_title');
		
		$data['text_no_results'] = $this->lang->line('text_no_results');

		$data['column_name'] = $this->lang->line('column_name');
		$data['column_action'] = $this->lang->line('column_action');
		
		$data['message_selections'] = $this->lang->line('message_selections');

		$data['button_insert'] = $this->lang->line('button_insert');
		$data['button_delete'] = $this->lang->line('button_delete');
		$data['button_edit'] = $this->lang->line('button_edit');
 
 		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
		if ($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');
		
			$this->session->unset_userdata('success');
		} else {
			$data['success'] = '';
		}
		
		
		$data['header'] = helper_load_header();
		$data['footer'] = helper_load_footer();
		
		$this->load->view('user/user_group_list', $data);
 	}

	private function getForm() {
		$data['heading_title'] = $this->lang->line('heading_title');
		
		$data['text_select_all'] = $this->lang->line('text_select_all');
		$data['text_unselect_all'] = $this->lang->line('text_unselect_all');
				
		$data['entry_name'] = $this->lang->line('entry_name');
		$data['entry_access'] = $this->lang->line('entry_access');
		$data['entry_modify'] = $this->lang->line('entry_modify');
		
		$data['button_save'] = $this->lang->line('button_save');
		$data['button_cancel'] = $this->lang->line('button_cancel');

		$data['tab_general'] = $this->lang->line('tab_general');

 		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
 		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = '';
		}
		
  		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),
      		'separator' => false
   		);

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('heading_title'),
			'href'      => site_url('user/user_permission'),
      		'separator' => ' :: '
   		);
			
		if ((int)$this->uri->segment(4) == 0) {
			$data['action'] = site_url('user/user_permission/insert');
		} else {
			$data['action'] = site_url('user/user_permission/update/' . (int)$this->uri->segment(4));
		}
		  
    	$data['cancel'] = site_url('user/user_permission');

		if (((int)$this->uri->segment(4) != 0) && $this->input->server('REQUEST_METHOD') != 'POST') {
			$user_group_info = $this->Model_User_Group->getUserGroup((int)$this->uri->segment(4));
		}

		if ($this->input->post('name') !== false) {
			$data['name'] = $this->input->post('name');
		} elseif (isset($user_group_info)) {
			$data['name'] = $user_group_info['name'];
		} else {
			$data['name'] = '';
		}
		
		/*
		$ignore = array(
			'common/home',
			'common/startup',
			'common/login',
			'common/logout',
			'common/forgotten',
			'common/reset',			
			'error/not_found',
			'error/permission',
			'common/footer',
			'common/header'
		);
				
		$data['permissions'] = array();
		
		$files = glob(APPPATH . 'controller/*'.'/*.php');
		
		foreach ($files as $file) {
			$data = explode('/', dirname($file));
			
			$permission = end($data) . '/' . basename($file, '.php');
			
			if (!in_array($permission, $ignore)) {
				$data['permissions'][] = $permission;
			}
		}
		
		if (isset($this->request->post['permission'])) {
			$data['access'] = $this->request->post['permission']['access'];
		} elseif (isset($user_group_info['permission']['access'])) {
			$data['access'] = $user_group_info['permission']['access'];
		} else { 
			$data['access'] = array();
		}

		if (isset($this->request->post['permission'])) {
			$data['modify'] = $this->request->post['permission']['modify'];
		} elseif (isset($user_group_info['permission']['modify'])) {
			$data['modify'] = $user_group_info['permission']['modify'];
		} else { 
			$data['modify'] = array();
		}
		*/
		
		$data['header'] = helper_load_header();
		$data['footer'] = helper_load_footer();
						
		$this->load->view('user/user_group_form', $data);	
	}

	private function validateForm() {
		if (!$this->Model_User->has_permission('user_permission_edit', $this->session->userdata('user_id'))) {
			$this->error['warning'] = $this->lang->line('error_permission');
		}

		if ((strlen(utf8_decode($this->input->post('name'))) < 3) || (strlen(utf8_decode($this->input->post('name'))) > 64)) {
			$this->error['name'] = $this->lang->line('error_name');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->Model_User->has_permission('user_permission_delete', $this->session->userdata('user_id'))) {
			$this->error['warning'] = $this->lang->line('error_permission');
		}
		
		$this->load->model('user/Model_User');
      	
		$selected = explode('-', $this->input->post('selected'));
		foreach ($selected as $user_group_id) {
			$user_total = $this->Model_User->getTotalUsersByGroupId($user_group_id);

			if ($user_total) {
				$this->error['warning'] = sprintf($this->lang->line('error_user'), $user_total);
				exit($this->error['warning']);
			}
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>